package com.lcn.tm.service.redis;

import com.lcn.comm.domain.TxGroup;
import com.lcn.comm.service.redis.RedisService;
import com.lcn.comm.util.PoolUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>类说明</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @create: 下午8:45
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 异步设置缓存
     *
     * @param key
     * @param obj
     */
    public void setValue(final String key, final Object obj) {
        PoolUtil.getPool().execute(new Runnable() {
            @Override
            public void run() {
                redisTemplate.opsForValue().set(key, obj, 30, TimeUnit.SECONDS);
            }
        });
    }

    /**
     * 异步删除缓存
     *
     * @param key
     */
    public void remove(final String key) {
        PoolUtil.getPool().execute(new Runnable() {
            @Override
            public void run() {
                redisTemplate.delete(key);
            }
        });
    }

    /**
     * 从缓存获取事务组
     *
     * @param key
     * @return
     */
    public TxGroup getTxGroupByKey(String key) {
        return (TxGroup) redisTemplate.opsForValue().get(key);
    }
}
